package concurrent.result;

import concurrent.domain.Result;
import concurrent.domain.Slave;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ResultByExecutorService {

    private ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            2,
            3, 30l,
            TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>(),
            new ThreadPoolExecutor.AbortPolicy());

    /**
     * 不需要返回值，使用execute
     */
    public void testExecute() throws InterruptedException {
        System.out.println(System.currentTimeMillis());
        Result<List<Slave>> listResult = new Result<>();
        listResult.setResult(new ArrayList<>(2));
        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                Slave slave = new Slave();
                slave.setName("黄狄");
                slave.setAge(22);
                slave.setDesc("黄狄喜欢大鸡巴");
                listResult.getResult().add(slave);
            }
        });

        threadPoolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                Slave slave = new Slave();
                slave.setName("周琴");
                slave.setAge(22);
                slave.setDesc("周琴的水很多");
                listResult.getResult().add(slave);
            }
        });
        //关闭线程池
        TimeUnit.SECONDS.sleep(3);
        threadPoolExecutor.shutdown();
        System.out.println(System.currentTimeMillis());
        System.out.println(System.currentTimeMillis() + ":" + listResult.getResult());

    }


    /**
     * 需要返回值，使用submit
     */
    public void testSubmit() throws ExecutionException, InterruptedException {
        System.out.println(System.currentTimeMillis());
        Future<List<Slave>> future1 = threadPoolExecutor.submit(() -> {
            ArrayList<Slave> slaves = new ArrayList<>();
            Slave slave = new Slave();
            slave.setName("黄狄");
            slave.setAge(22);
            slave.setDesc("黄狄喜欢大鸡巴");
            slaves.add(slave);
            Slave slave2 = new Slave();
            slave2.setName("潘依倩");
            slave2.setAge(22);
            slave2.setDesc("潘依倩的骚屁眼");
            slaves.add(slave2);
            return slaves;
        });

        Future<List<Slave>> future2 = threadPoolExecutor.submit(() -> {
            ArrayList<Slave> slaves = new ArrayList<>();
            Slave slave = new Slave();
            slave.setName("周琴");
            slave.setAge(22);
            slave.setDesc("周琴的水很多");
            slaves.add(slave);
            return slaves;
        });
        //关闭线程池
        threadPoolExecutor.shutdown();
        System.out.println(System.currentTimeMillis());
        List<Slave> slaves = future1.get();
        List<Slave> slaves1 = future2.get();
        slaves1.addAll(slaves);
        System.out.println(System.currentTimeMillis() + ":" + slaves1);
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ResultByExecutorService resultByExecutorService = new ResultByExecutorService();
        //创建一个线程池
//        userExecutorService.testExecute();
        resultByExecutorService.testSubmit();
    }
}
